<?php
/**
 * Created by PhpStorm.
 * User: 悟能科技
 * Date: 2018/10/13
 * Time: 10:16
 */

namespace app\common\controller;


use app\common\model\Merchant;
use app\common\model\Paylog;
use app\common\model\YdhOpen;
use think\Controller;

class Ydhwork extends Controller
{
//易订货配置


    /*
     *  获取accesstoken
     *  1546247411   e8d2910a99bd98b440d75c9d00e199061140225
     */
    public function get_access_token($top_mid){

        $YdhOpen = new YdhOpen();
        $time = time();
        $map['top_mid'] = $top_mid;
        $openinfo = $YdhOpen->where($map)->find();
        $expires_time = $openinfo['expires_in'] + $openinfo['expires_time']-2505600;
        if ($time > $expires_time) {
            $data = [
                'userName' => $openinfo['username'],
                'password' => $openinfo['password'],
                'client_id' => $openinfo['client_id'],
                'client_secret' => $openinfo['client_secret'],
                'grant_type' => $openinfo['grant_type'],
                'scope' => $openinfo['scope'],
            ];

            $url = 'https://api.dinghuo123.com/v2/oauth2/token?';
            $res = $this->http_get($url.http_build_query($data));
            $res = json_decode($res,true);
            if ($res['code'] == 200) {
                $datas['access_token'] = $res['data']['access_token'];
                $datas['expires_time'] = time();
                $datas['expires_in'] = $res['data']['expires_in'];
                $ress = $YdhOpen->allowField(true)->save($datas,$map);

                if ($ress){
                    $accesstoken = $datas['access_token'];
                }else{
                    $accesstoken = $datas['access_token'];
                }
            }else{
                $accesstoken = $openinfo['access_token'];
            }
        }else{
            $accesstoken = $openinfo['access_token'];
        }
        return $accesstoken;
    }

    /*
     *
     *  新增客户
     *
     */
    public function create_user($param){
        $url = 'https://api.dinghuo123.com/v2/customer/customer_create.json?';
        $data = $param;
        $data['access_token'] = $this->get_access_token($param['top_mid']);
        unset($data['top_mid']);
        $res =  $this->http_get($url.http_build_query($data));
        $res = json_decode($res,true);
        if ($res['code'] == 200){
            $rarr['code'] = $res['code'];
            $rarr['data'] = $res['data'];
            $rarr['message'] = $res['message'];
        }else{
            $rarr['code'] = $res['code'];
            $rarr['message'] = $res['message'];
        }
        return $rarr;

    }

    /*
    *
    *  更新客户
    *
    */
    public function update_user($param){

        $url = 'https://api.dinghuo123.com/v2/customer/customer_update.json?';
        $data = $param;
        $data['access_token'] = $this->get_access_token($param['top_mid']);
        unset($data['top_mid']);
        $ress =  $this->http_get($url.http_build_query($data));
        $res = json_decode($ress,true);
       return $res;
//        return $res;
        if ($res['code'] == 200){
            $rarr['code'] = 0;
            $rarr['message'] = $res['message'];
        }else{
            $rarr['code'] = $res['code'];
            $rarr['message'] = $res['message'];
        }
        return $rarr;

    }


    public function ydhinfo($param){
        $url = 'https://api.dinghuo123.com/v2/customer/customer_detail.json?';
        $data = $param;
        $data['access_token'] = $this->get_access_token($param['top_mid']);
        unset($data['top_mid']);
        halt($url.http_build_query($data));
        $res = $this->http_get($url.http_build_query($data));
        $res = json_decode($res,true);
        return $res;
        if ($res['code'] == 200){

            $rarr['code'] = 0;
            $rarr['data'] = $res['data'];
            $rarr['message'] = $res['message'];
        }else{
            $rarr['code'] = $res['code'];
            $rarr['message'] = $res['message'];
        }
        return $rarr;
    }

    //客户列表
    public function user_list($param){
        $url = 'https://api.dinghuo123.com/v2/customer/customer_list.json?';
        $data = $param;
        $data['access_token'] = $this->get_access_token($param['top_mid']);
        unset($data['top_mid']);
        halt($url.http_build_query($data));
        $res = $this->http_get($url.http_build_query($data));
        $res = json_decode($res,true);
        return $res;
    }

    // 更改客户等级
    private function upgradeuser($param){
//        $YdhUser = new YdhOpen();
//        $userinfo = $YdhUser->where(['openid'=>$param['openid']])->find();
//        //END
//        $data['name'] = $userinfo['name'];
//        $data['contactor'] = $userinfo['name'];
//        $data['mobile'] = $userinfo['phone'];
//        $data['customertypeId'] = 2;
//        $YdhUser->allowField(true)->save($data);
//        $ares = $this->update_user($data);
//        if ($ares['code'] == 200){
//            $YdhUser->allowField(true)->save($data,['openid'=>$userinfo['openid']]);
//        }
    }

    /*
     *
     * 获取客户类型
     *
     */
    public function get_user_cate($param){
        $url = 'https://api.dinghuo123.com/v2/customer/customer_type_list.json?';
        $data['access_token'] = $this->get_access_token($param);
        $res = $this->http_get($url.http_build_query($data));
        $res = json_decode($res,true);
        return $res;
    }


    //
    public function get_top_mid($mid){
        $Merchant = new Merchant();
        $top_mid = $Merchant->where(['id'=>$mid])->value('top_mid');
        return $top_mid;
    }
    /*
     *
     *  客户修改
     *
     */


    private function http_get($url)
    {
        $oCurl = curl_init();
        if (stripos($url, "https://") !== FALSE) {
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }
        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
        $sContent = curl_exec($oCurl);
        $aStatus = curl_getinfo($oCurl);
        curl_close($oCurl);
        if (intval($aStatus["http_code"]) == 200) {
            return $sContent;
        } else {
            return false;
        }
    }

    private function http_post($url, $param, $post_file = false)
    {
        $oCurl = curl_init();
        if (stripos($url, "https://") !== FALSE) {
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }
        if (PHP_VERSION_ID >= 50500 && class_exists('\CURLFile')) {
            $is_curlFile = true;
        } else {
            $is_curlFile = false;
            if (defined('CURLOPT_SAFE_UPLOAD')) {
                curl_setopt($oCurl, CURLOPT_SAFE_UPLOAD, false);
            }
        }
        if (is_string($param)) {
            $strPOST = $param;
        } elseif ($post_file) {
            if ($is_curlFile) {
                foreach ($param as $key => $val) {
                    if (substr($val, 0, 1) == '@') {
                        $param[$key] = new \CURLFile(realpath(substr($val, 1)));
                    }
                }
            }
            $strPOST = $param;
        } else {
            $aPOST = array();
            foreach ($param as $key => $val) {
                $aPOST[] = $key . "=" . urlencode($val);
            }
            $strPOST = join("&", $aPOST);
        }
        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($oCurl, CURLOPT_POST, true);
        curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);
        $sContent = curl_exec($oCurl);
        $aStatus = curl_getinfo($oCurl);
        curl_close($oCurl);
        if (intval($aStatus["http_code"]) == 200) {
            return $sContent;
        } else {
            return false;
        }
    }
}